/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_general.h"
#include "rep_logs_pages.h"
#include "rep_logs_hyperlinks.h"
#include "rep_logs_independent_pages.h"
#include "rep_logs_referers.h"
#include "rep_logs_keywords.h"
#include "rep_logs_dates.h"
#include "rep_logs_graphs.h"
#include "rep_logs_directories.h"
#include "rep_logs_blinks.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_logs_summary_html_file(GraphFrame *gf,FILE *fp,WebStatPtr msn);

void
write_table_http_code(FILE *fp,WebStatPtr msn);

void
write_table_http_method(FILE *fp,WebStatPtr msn);

void
write_table_http_version(FILE *fp,WebStatPtr msn);

int
write_body_index_of_log_reports(GraphFrame *gf, FILE *fp, WebStatPtr msn,
				char *prename, char *prefix,int nobjects,
				int nkeywords, int ngraphs, int minedges,
				int nusers, int maxedges);
void
write_body_domains_html_file(FILE *fp,WebStatPtr msn);

int 
report_logs_summary(GraphFrame *gf, char *name_file,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Logs Summary Report");
  write_start_body_html_file(fp,"#ffffff");
  write_body_logs_summary_html_file(gf,fp,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_logs_summary_html_file(GraphFrame *gf,FILE *fp,WebStatPtr msn)
{
  LNode_type *lsites = LOGR_get_list_sites(msn);
  LNode_type *lurl_ext_ref = LOGR_get_list_url_external_referers(msn);
  LNode_type *lurl_int_ref = LOGR_get_list_url_internal_referers(msn);
  int tnurlextref = lurl_ext_ref ? count_llist(lurl_ext_ref) : 0;
  int tnurlintref = lurl_int_ref ? count_llist(lurl_int_ref) : 0;

  LNode_type *luas = LOGR_get_list_ua(msn);
  LNode_type *lspiders = LOGR_get_list_spider(msn);
  int tnuas = luas ? count_llist(luas) : 0;
  int tnspiders = lspiders ? count_llist(lspiders) : 0;

  Hashtable_type *key_table = msn ? LOGR_get_table_keywords(msn) : NULL;
  LNode_type *lkeys = key_table ? get_list_elements(*key_table) : NULL;
  long tsearch = LOGR_get_total_number_searches(msn);
  int tnkeys = lkeys ? count_llist(lkeys) : 0;

  LNode_type *lusers = LOGR_get_list_users(msn);
  int tusers = lusers ? count_llist(lusers) : 0;
  short cus = LOGR_get_compute_user_sessions(msn);
  int ninobj = count_llist(get_list_objects(gf));
  int ext_log = LOGR_get_logs_filetype(msn);

  char str[100];
  int tnsites = lsites ? count_llist(lsites) : 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);

  int nhtml = 0;
  int nonhtml = 0;
  int nobj = 0;
  int nhyp_html = 0;
  int nhyp_nhtml = 0;
  int nehtml = 0;
  int nenhtml = 0;
  int nhost = 0;
  int nsa = 0;
  double arh=0, ard=0,ash=0,asd=0;

  time_t now;

  LOGR_get_total_number_pages(msn,gf,&nhtml,&nonhtml,&nobj);
  LOGR_get_total_number_hyperlinks(msn,gf,&nhyp_html,&nhyp_nhtml);
  LOGR_get_total_number_entry_pages(msn,gf,&nehtml,&nenhtml);
  LOGR_get_total_number_referers(msn,gf,&nhost,&nsa);
  LOGR_get_average_by_time(msn,&ard,&arh,&asd,&ash);
  
  now = time(NULL);
  write_header_html_file(fp,1,"Logs Summary Report");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_horizontal_rule_html_file(fp);

  write_start_table_html_file(fp, 2);

  write_row_data(fp, "Number of Requests",tacc);
  sprintf(str,"%.2f K Bytes",(float)(tsize/1024.0));
  if(cus && ext_log == LOGR_EXTENDED_LOG)
      write_row_data(fp, "Number of User Sessions",tusers);
  write_row_string(fp, "Total Size",str);
  write_row_data(fp, "Number of Unique HTML Pages",nhtml);
  write_row_data(fp, "Number of Unique Non HTML Pages",nonhtml);
  if(ninobj > 0)
      write_row_data(fp, "Number of Unique Inline Objects",nobj);
  if(ext_log == LOGR_EXTENDED_LOG) {
      write_row_data(fp, "Number of Unique Hyperlinks to HTML Pages",nhyp_html);
      write_row_data(fp, "Number of Unique Hyperlinks to Non HTML Pages",nhyp_nhtml);
      write_row_data(fp, "Number of Unique HTML Entry Pages",nehtml);
      write_row_data(fp, "Number of Unique Non HTML Entry Pages",nenhtml);
  }
  write_row_data(fp, "Number of Unique Sites", tnsites);
  if(ext_log == LOGR_EXTENDED_LOG) {
      write_row_data(fp, "Number of Unique Host Referers",nhost);
      write_row_data(fp, "Number of Unique Search Engine Referers",nsa);
      write_row_data(fp, "Number of Unique External URL Referers",tnurlextref);
      write_row_data(fp, "Number of Unique Internal URL Referers",tnurlintref);
      write_row_data(fp, "Number of Unique Browsers (User Agents)",tnuas-tnspiders);
      write_row_data(fp, "Number of Unique Spiders (User Agents)",tnspiders);
  }
  sprintf(str,"%.2f",arh);
  write_row_string(fp, "Number of Requests per Hour",str);
  sprintf(str,"%.2f",ard);
  write_row_string(fp, "Number of Requests per Day",str);
  sprintf(str,"%.2f KBytes",ash/1024.0);
  write_row_string(fp, "Number of KBytes per Hour",str);
  sprintf(str,"%.2f KBytes",asd/1024.0);
  write_row_string(fp, "Number of KBytes per Day",str);
  if(ext_log == LOGR_EXTENDED_LOG) {
      write_row_data(fp, "Number of Searches",tsearch);
      write_row_data(fp, "Number of Unique Keywords",tnkeys);
  }
  write_end_table_html_file(fp); 
  write_horizontal_rule_html_file(fp);
  write_table_http_code(fp,msn);
  write_horizontal_rule_html_file(fp);
  write_table_http_method(fp,msn);
  write_horizontal_rule_html_file(fp);
  write_table_http_version(fp,msn);
}

void
write_table_http_code(FILE *fp,WebStatPtr msn)
{

  LNode_type *lcodes = LOGR_get_alist_codes(msn);
  LNode_type *ptr = NULL;
  WebGenInfoPtr *varray = NULL;
  int i = 0;
  int tncodes = tncodes ? count_llist(lcodes) : 0;

  if(tncodes <= 0) 
      return;

  varray = tncodes ? (WebGenInfoPtr*)calloc(tncodes,
					   sizeof(WebGenInfoPtr)) : NULL;

  if(!varray && tncodes > 0) {
      fprintf(stderr,"Out of Memory write_table_http_code\n");
      abort();
  }

  for(ptr = lcodes->back; ptr != lcodes; ptr = ptr->back)
  {
      WebGenInfoPtr gn = (WebGenInfoPtr)ptr->info;
      varray[i++]=gn;
  }

  write_header_html_file(fp,2,"Requests by HTTP Code");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"HTTP Code","Requests","HTML Pages", "Bytes");

  for(i = 1; i<=tncodes ; i++)
  {
    WebGenInfoPtr gn = (WebGenInfoPtr)randomized_select((GPTR *)varray,
				       i-1,tncodes-1,1,
				       LOGR_is_context_gen_info_bigger,
				       LOGR_is_context_gen_info_lower);

    int code = (int)LOGR_get_gen_info_context(gn);
    long acc = LOGR_get_gen_info_naccesses(gn);
    long npages = LOGR_get_gen_info_npages(gn);
    double ssize =  LOGR_get_gen_info_sizebytes(gn);

    const char *str = LOGR_get_string_code(code);
    char tstr[100],tstr2[20],tstr3[20],tstr4[20];
    sprintf(tstr2,"%.2f K",(float)(ssize/1024.0));
    sprintf(tstr3,"%ld",acc);
    sprintf(tstr4,"%ld",npages);
    if(str)
	write_row_string3(fp,str,tstr3,tstr4,tstr2);
    else
    {
	sprintf(tstr,"%d",code);
	write_row_string3(fp,tstr,tstr3,tstr4,tstr2);
    }
  }

  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
}

void
write_table_http_method(FILE *fp,WebStatPtr msn)
{

  LNode_type *lmethods = LOGR_get_alist_methods(msn);
  LNode_type *ptr = NULL;
  WebGenInfoPtr *varray = NULL;
  int i = 0;
  int tnmethods = tnmethods ? count_llist(lmethods) : 0;

  if(tnmethods <= 0) 
      return;

  varray = tnmethods ? (WebGenInfoPtr*)calloc(tnmethods,
					   sizeof(WebGenInfoPtr)) : NULL;

  if(!varray && tnmethods > 0) {
      fprintf(stderr,"Out of Memory write_table_http_method\n");
      abort();
  }

  for(ptr = lmethods->back; ptr != lmethods; ptr = ptr->back)
  {
      WebGenInfoPtr gn = (WebGenInfoPtr)ptr->info;
      varray[i++]=gn;
  }

  write_header_html_file(fp,2,"Requests by HTTP Method");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"HTTP Method","Requests","HTML Pages", "Bytes");

  for(i = 1; i<=tnmethods ; i++)
  {
    WebGenInfoPtr gn = (WebGenInfoPtr)randomized_select((GPTR *)varray,
				       i-1,tnmethods-1,1,
				       LOGR_is_context_gen_info_bigger,
				       LOGR_is_context_gen_info_lower);

    int method = (int)LOGR_get_gen_info_context(gn);
    long acc = LOGR_get_gen_info_naccesses(gn);
    long npages = LOGR_get_gen_info_npages(gn);
    double ssize =  LOGR_get_gen_info_sizebytes(gn);

    const char *str = LOGR_get_string_method(method);
    char tstr[100],tstr2[20],tstr3[20],tstr4[20];
    sprintf(tstr2,"%.2f K",(float)(ssize/1024.0));
    sprintf(tstr3,"%ld",acc);
    sprintf(tstr4,"%ld",npages);
    if(str)
	write_row_string3(fp,str,tstr3,tstr4,tstr2);
    else
    {
	sprintf(tstr,"%d",method);
	write_row_string3(fp,tstr,tstr3,tstr4,tstr2);
    }
  }

  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
}

void
write_table_http_version(FILE *fp,WebStatPtr msn)
{

  LNode_type *lversions = LOGR_get_alist_versions(msn);
  LNode_type *ptr = NULL;
  WebGenInfoPtr *varray = NULL;
  int i = 0;
  int tnversions = tnversions ? count_llist(lversions) : 0;

  if(tnversions <= 0) 
      return;

  varray = tnversions ? (WebGenInfoPtr*)calloc(tnversions,
					   sizeof(WebGenInfoPtr)) : NULL;

  if(!varray && tnversions > 0) {
      fprintf(stderr,"Out of Memory write_table_http_version\n");
      abort();
  }

  for(ptr = lversions->back; ptr != lversions; ptr = ptr->back)
  {
      WebGenInfoPtr gn = (WebGenInfoPtr)ptr->info;
      varray[i++]=gn;
  }

  write_header_html_file(fp,2,"Requests by HTTP Version");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"HTTP Version","Requests","HTML Pages", "Bytes");

  for(i = 1; i<=tnversions ; i++)
  {
    WebGenInfoPtr gn = (WebGenInfoPtr)randomized_select((GPTR *)varray,
				       i-1,tnversions-1,1,
				       LOGR_is_context_gen_info_bigger,
				       LOGR_is_context_gen_info_lower);

    int version = (int)LOGR_get_gen_info_context(gn);
    long acc = LOGR_get_gen_info_naccesses(gn);
    long npages = LOGR_get_gen_info_npages(gn);
    double ssize =  LOGR_get_gen_info_sizebytes(gn);

    const char *str = LOGR_get_string_version(version);
    char tstr[100],tstr2[20],tstr3[20],tstr4[20];
    sprintf(tstr2,"%.2f K",(float)(ssize/1024.0));
    sprintf(tstr3,"%ld",acc);
    sprintf(tstr4,"%ld",npages);
    if(str)
	write_row_string3(fp,str,tstr3,tstr4,tstr2);
    else
    {
	sprintf(tstr,"%d",version);
	write_row_string3(fp,tstr,tstr3,tstr4,tstr2);
    }
  }

  write_end_table_html_file(fp); 

  free(varray); varray = NULL; 
}





char *
construct_full_prefix(char *prefix, char *dir)
{
    char *wdir= NULL;
    char *prename = NULL;
    if(!prefix)
	return NULL;
    if(dir && dir[0]!='\0')
    {
	if(dir[0]!='/')
	{
	    wdir = getcwd(NULL,200);	
	    GRStrAllocCopy(prename,wdir);
	    GRStrAllocCat(prename,"/");
	    if(wdir) {
		free(wdir); wdir = NULL;
	    }
	}

	GRStrAllocCat(prename,dir);
	GRStrAllocCat(prename,"/");
    }
    GRStrAllocCat(prename,prefix);
    return prename;
}

int
report_index_of_log_reports(GraphFrame *gf, WebStatPtr msn, char *prefix, 
			    char *dir,int nobjects, int nkeywords, 
			    int ngraphs, int minedges, int nusers, 
			    int maxedges)
{
  char name_file[100];
  int status = 0;
  FILE *fp = NULL;

  char *prename = prefix && dir ? construct_full_prefix(prefix,dir) : NULL;

  if(!prename)
      return -2;

  sprintf(name_file,"%s_log_report_index.html", prename);
  fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Index of Log Reports of Web Site");
  status = write_body_index_of_log_reports(gf,fp,msn, prename,prefix,nobjects,
					   nkeywords, ngraphs,
					   minedges, nusers,maxedges);

  write_end_html_file(fp);
  fclose(fp);
  return status;
}


int
write_body_index_of_log_reports(GraphFrame *gf, FILE *fp, WebStatPtr msn,
				char *prename, char *prefix,int nobjects,
				int nkeywords, int ngraphs, int minedges,
				int nusers, int maxedges)
{
  char name_file[500];
  char rel_file[100];
  char title[500];
  int status = 0;
  int nobj = count_llist(get_list_objects(gf));
  const char *bname = LOGR_get_base_webhost(msn);
  const char *url_prefix = LOGR_get_url_prefix(msn);
  short cus = LOGR_get_compute_user_sessions(msn);
  int ext_log = LOGR_get_logs_filetype(msn);
  time_t now;

  now = time(NULL);

  if(!prename)
      return -1;

  write_start_body_html_file(fp,"#ffffff");
  if(url_prefix)
      sprintf(title,"Index of Log Reports of Web Site %s", url_prefix);
  else if(bname)
      sprintf(title,"Index of Log Reports of Web Site %s", bname);
  else
      sprintf(title,"Index of Log Reports of Web Site");

  write_header_html_file(fp,1,title);
  write_blank_spaces_html_file(fp,4);
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_break_html_file(fp);
  write_start_ul_html_file(fp);

  sprintf(name_file,"%slogsummary.html",prename);
  sprintf(rel_file,"%slogsummary.html",prefix);
  report_logs_summary(gf, name_file,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Log Summary");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);

  sprintf(name_file,"%sdatestat.html",prename);
  sprintf(rel_file,"%sdatestat.html",prefix);
  report_date_statistics(name_file,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Date Statistics");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RSDString);

  sprintf(name_file,"%smrpages.html",prename);
  sprintf(rel_file,"%smrpages.html",prefix);
  report_spages_most_requested(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Most Requested Pages");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  if(nobj > 0)
      fprintf(fp,RMRPString);
  else
      fprintf(fp,RMRPString2);

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%smentrypages.html",prename);
  sprintf(rel_file,"%smentrypages.html",prefix);
  report_entry_pages_most_requested(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Most Requested Entry Pages");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RMVEPString);
  }

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%smvhyperlinks.html",prename);
  sprintf(rel_file,"%smvhyperlinks.html",prefix);
  report_shyperlinks_most_visited(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Most Visited Hyperlinks");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RMVSHString);
  }

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%smvblinks.html",prename);
  sprintf(rel_file,"%smvblinks.html",prefix);
  report_top_broken_links(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Most Visited Broken Links");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RMVBLString);
  }

  sprintf(name_file,"%smrsites.html",prename);
  sprintf(rel_file,"%smrsites.html",prefix);
  report_sites_most_accesses(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Sites with Most Access Count");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RSMAString);

  sprintf(name_file,"%smrdomains.html",prename);
  sprintf(rel_file,"%smrdomains.html",prefix);
  report_logs_domains(name_file,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Domains with Most Access Count");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RDNMAString);

  
  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%sturlreferer.html",prename);
  sprintf(rel_file,"%sturlreferer.html",prefix);
  report_top_url_referers(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Top URL Referers");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RTURString);
  }

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%sthostreferer.html",prename);
  sprintf(rel_file,"%sthostreferer.html",prefix);
  report_top_referers(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Top Host and Search Engine Referers");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RTHRString);
  }

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%stkeywords.html",prename);
  sprintf(rel_file,"%stkeywords.html",prefix);
  report_top_keywords(gf, name_file,nkeywords,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Top Keywords");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RTKString);
  }

  sprintf(name_file,"%smrdir.html",prename);
  sprintf(rel_file,"%smrdir.html",prefix);
  report_directories_most_accesses(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Directories with Most Access Count");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RDMAString);

  if(ext_log == LOGR_EXTENDED_LOG) {
  sprintf(name_file,"%smbrowsers.html",prename);
  sprintf(rel_file,"%smbrowsers.html",prefix);
  report_pages_most_browsers_requested(gf, name_file,nobjects,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Browsers with Most Access Count");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RBAString);
  }

  if(cus) {
  if(ext_log == LOGR_EXTENDED_LOG && ngraphs > 0) {
  sprintf(name_file,"%stgraphs.html",prename);
  sprintf(rel_file,"%stgraphs.html",prefix);
  report_graphs_most_visited(gf, name_file,ngraphs,minedges,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Graphs with Most User Sessions");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RMVGString);
  }

  if(ext_log == LOGR_EXTENDED_LOG && nusers > 0) {
  sprintf(name_file,"%stusers.html",prename);
  sprintf(rel_file,"%stusers.html",prefix);
  report_users_most_requests(gf, name_file,nusers,maxedges,msn);
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,rel_file);
  fprintf(fp,"Users with Longest Sessions");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,RMVUString);
  }
  }
  
  write_end_ul_html_file(fp);
  write_end_body_html_file(fp);

  if(prename)  {	
      free(prename); prename = NULL;
  }
  return status;
}

int 
report_logs_domains(char *name_file,WebStatPtr msn)
{  
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Domains with Most Access Count ");
  write_start_body_html_file(fp,"#ffffff");
  write_body_domains_html_file(fp,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_domains_html_file(FILE *fp,WebStatPtr msn)
{
  LNode_type *ldomains = LOGR_get_list_domains(msn);
  LNode_type *ptr = NULL;
  WebGenInfoPtr *varray = NULL;
  int i=0;
  char ntitle[100];
  int tndomains = ldomains ? count_llist(ldomains) : 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  varray = tndomains ? (WebGenInfoPtr *)calloc(tndomains,sizeof(WebGenInfoPtr)) : NULL;

  if(!varray && tndomains > 0) {
      fprintf(stderr,"Out of Memory write_body_domains_html_file\n");
      abort();
  }

  for(ptr = ldomains->back; ptr != ldomains; ptr=ptr->back)
  {
    WebGenInfoPtr mgi = (WebGenInfoPtr)ptr->info;
    varray[i++] = mgi;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Domains with Most Access Count");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Domains : ");
  fprintf(fp, " %d Domains\n",tndomains);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RDNMAString);
  write_horizontal_rule_html_file(fp);

  if(tndomains > 0) {

  sprintf(ntitle,"%d Domains with Most Access Count", tndomains);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string7(fp,"Num","Domain","Code","Access Count",
			     "Percentage Access Count", "HTML Pages",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tndomains; i++)
  {
    WebGenInfoPtr mgi= (WebGenInfoPtr)
	randomized_select((GPTR *)varray,i-1,tndomains-1,1,
			  LOGR_is_access_gen_info_lower,
			  LOGR_is_access_gen_info_bigger);
    if(mgi)
    {
	const char *lcode = (char *)LOGR_get_gen_info_context(mgi);
	char *ldomain = LOGR_get_long_country_code((char *)lcode);
	long scount = LOGR_get_gen_info_naccesses(mgi);
	char snum[20],str[20], str2[20], str3[20], str4[20],str5[20];
	double ssize = LOGR_get_gen_info_sizebytes(mgi);
	long npages = LOGR_get_gen_info_npages(mgi);
	double ave_size = (double)(ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	if(!ldomain)
	    ldomain = "Unknown";
	sprintf(snum,"%d",i);
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",npages);
	sprintf(str2,"%.2f K",(float)(ssize/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_num_string7(fp,snum,(char *)ldomain,lcode,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
}


